package chapter2;

/**
 * Created by LinGer on 2019/6/17
 */
public class MyRunnable implements Runnable {

    private int ticket = 10;

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if(this.ticket > 0){
                //Runnable接口没有getName方法
                System.out.println(Thread.currentThread().getName() + "卖票: ticket" + this.ticket--);
            }
        }
    }

    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();

        //3个线程共用一个Runnable对象
        Thread t1 = new Thread(myRunnable);
        Thread t2 = new Thread(myRunnable);
        Thread t3 = new Thread(myRunnable);

        t1.start();
        t2.start();
        t3.start();
    }
}
